﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using ebay.Dal.CenterDB_EbayTableAdapters;
using System.Data;
using ebay.Utility;
using System.Drawing;

namespace ebay.Dal
{
  public class EbaySelling
  {

      public bool Add(string ebayUserId, List<ebay.Model.EbaySelling> listEbaySelling) {
          string cmdText1 = "Select SellingStatus From EbaySelling Where OrderLineItemId = '{0}' And EbayUserId = '{1}'";
          string cmdText3 = "Delete From EbaySelling Where OrderLineItemId = '{0}' And EbayUserId = '{1}';";
          using (SqlConnection connection = new SqlConnection(SqlHelper.ceterDB_Conn)) {
              connection.Open();
              T_EbaySellingTableAdapter ebaySellingAdapter = new T_EbaySellingTableAdapter();
              ebaySellingAdapter.Connection = connection;
              using (SqlTransaction trans = connection.BeginTransaction()) {
                  ebaySellingAdapter.Transaction = trans;
                  foreach (ebay.Model.EbaySelling eSelling in listEbaySelling) {
                      try {

                          //查询本条订单在数据库是否存在,如果存在则会返回改订单的状态
                          object val = SqlHelper.ExecuteScalar(trans, CommandType.Text,
                                                               string.Format(cmdText1, eSelling.OrderLineItemId, eSelling.EbayUserId),
                                                               null);

                          if (val != null && val != DBNull.Value) {
                              //如果该订单的状态为未匹配,则删除
                              if (val.ToString() == "1") {
                                  SqlHelper.ExecuteNonQuery(trans, CommandType.Text,
                                                            string.Format(cmdText3, eSelling.OrderLineItemId, eSelling.EbayUserId),
                                                            null);
                              }
                                  //否则跳过,不执行插入动作
                              else {
                                  continue;
                              }
                          }

                          //新增订单记录
                          ebaySellingAdapter.Insert(eSelling.EbayUserId, eSelling.OrderLineItemId, eSelling.OrderId, eSelling.OrderSalesRecordNumber, eSelling.SalesRecordNumber,
                                                    eSelling.UserId, eSelling.BuyerFullName, eSelling.BuyerEmail, eSelling.BuyerAddress1, eSelling.BuyerAddress2, eSelling.BuyerCity, eSelling.BuyerState,
                                                    eSelling.BuyerPostcode, eSelling.BuyerCountry, eSelling.ItemId, eSelling.TransactionID, eSelling.ItemTitle, eSelling.Quantity, (decimal)eSelling.SalePrice,
                                                    (decimal)eSelling.FinalValueFee, eSelling.IncludedVATRate, (decimal)eSelling.PostageAndPackaging, (decimal)eSelling.Insurance, (decimal)eSelling.TotalPrice, eSelling.PaymentMethod,
                                                    eSelling.PayPalTransactionID, eSelling.InvoiceNumber, eSelling.InvoiceDate, eSelling.SaleDate, eSelling.CheckoutDate, eSelling.PaidonDate,
                                                    eSelling.DispatchDate, eSelling.PostageService, eSelling.FeedbackLeft, eSelling.FeedbackReceived, eSelling.NotestoYourself, eSelling.CustomLable,
                                                    eSelling.PrivateNotes, eSelling.ProductIDType, eSelling.ProductIDValue, eSelling.ProductIDValue2, eSelling.VariationSKU, eSelling.Variation_Name,
                                                    eSelling.Variation_Value, eSelling.VariationTitle, eSelling.VariationViewItemURL, eSelling.EBayPaymentStatus, eSelling.BuyerSelectedShipping,
                                                    eSelling.FeedbackScore, (decimal)eSelling.PositiveFeedbackPercent, eSelling.ExternalTransactionTime, (decimal)eSelling.FeeOrCreditAmount,
                                                    (decimal)eSelling.PaymentOrRefundAmount, eSelling.Platform, eSelling.CompleteStatus, eSelling.CheckoutStatus, eSelling.LastTimeModified,
                                                    eSelling.SellingStatus, eSelling.MatchDate, eSelling.ImportDate, eSelling.BuyerCounty, eSelling.BuyerPhone, 0);
                      } catch (Exception) {
                          return false;
                      }
                  }

                  try {
                      trans.Commit();
                      return true;
                  } catch (Exception) {
                      trans.Rollback();
                  }
                  return true;
              }
          }
      }

    public DataTable GeteBayAddress(String eBayUserID, String orderID) {
      String sqlString = @"SELECT BuyerFullName AS BuyerName,BuyerAddress1 + ' ' + BuyerAddress2 AS BuyerAddress,BuyerCity,BuyerCountry,BuyerPostcode AS BuyerZipCode,BuyerEmail FROM eBaySelling WHERE eBayUserID=@eBayUserID AND OrderID=@OrderID";
      SqlParameter[] parameters = new SqlParameter[] {
        new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
        new SqlParameter() { ParameterName = "@OrderID", SqlDbType = SqlDbType.NVarChar, Size = 30, Value = orderID }
      };

      return SqlHelper.ExecuteDataTable(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
    }
  }
}
